<?php

//为了防止重复定义，可以在定义一个新的类的时候判断一下这个类是否已经存在：
if (class_exists('form') != true) {

    class form {

        /**
         * input
         * @param $name name
         * @param $value 默认值 如：TickyCMS
         * @param $required  是否为必填项 默认false
         * @param $width  宽度 如：100
         */
        public static function input($name = '', $value = '', $required = false, $width = 0) {
            $string = '<input class="form-control" ';
            if ($width)
                $string .= ' style="width:' . $width . 'px" ';
            if ($required)
                $string .= ' required="required" ';
            $string .= ' name="' . $name . '" id="' . $name . '" ';
            $string .= ' type="text" value="' . $value . '">';
            return $string;
        }

        /**
         * textarea
         * @param $name name
         * @param $value 默认值 如：TickyCMS
         * @param $required  是否为必填项 默认false
         * @param $width  宽度 如：100
         */
        public static function textarea($name = '', $value = '', $required = false, $width = 0) {
            $string = '<textarea name="' . $name . '" id="' . $name . '" ';
            if ($width)
                $string .= ' width="' . $width . 'px" ';
            if ($required)
                $string .= ' required="required" ';
            $string .= '>' . $value . '</textarea>';
            return $string;
        }

        /**
         * 下拉选择框
         */
        public static function select($name, $val = 0, $array = array(), $default_option = '') {
            $string = '<select ' . $name . '  class="form-control">';
            $default_selected = (empty($val) && $default_option) ? 'selected' : '';
            if ($default_option)
                $string .= "<option value='' $default_selected>$default_option</option>";
            if (!is_array($array) || count($array) == 0) {
                return false;
            }
            $ids = array();
            if (isset($val)) {
                $ids = explode(',', $val);
            }
            foreach ($array as $key => $value) {
                $selected = in_array($key, $ids) ? 'selected' : '';
                $string .= '<option value="' . $key . '" ' . $selected . '>' . $value . '</option>';
            }
            $string .= '</select>';
            return $string;
        }

        /**
         * 复选框
         *
         * @param $name name
         * @param $val 默认选中值，多个用 '逗号'分割 如：'1,2'
         * @param $array 一维数组 如：array('交易成功', '交易失败', '交易结果未知');
         */
        public static function checkbox($name, $val = '', $array = array()) {
            $string = '';
            $val = trim($val);
            if ($val != '')
                $val = strpos($val, ',') ? explode(',', $val) : array($val);
            $i = 1;
            foreach ($array as $value) {
                $value = trim($value);
                $checked = ($val && in_array($value, $val)) ? 'checked' : '';
                $string .= '<label class="option_label option_box" >';
                $string .= '<input type="checkbox" name="' . $name . '[]" id="' . $name . '_' . $i . '" ' . $checked . ' value="' . $value . '">' . $value;
                $string .= '</label>';
                $i++;
            }
            return $string;
        }

        /**
         * 单选框
         *
         * @param $name name
         * @param $val 默认选中值 如：1
         * @param $array 一维数组 如：array('交易成功', '交易失败', '交易结果未知');
         */
        public static function radio($name, $val = '', $array = array()) {
            $string = '';
            foreach ($array as $value) {
                $checked = trim($val) == trim($value) ? 'checked' : '';
                $string .= '<label class="option_label option_radio" >';
                $string .= '<input type="radio" name="' . $name . '" id="' . $name . '_' . $value . '" ' . $checked . ' value="' . $value . '">' . $value;
                $string .= '</label>';
            }
            return $string;
        }

        /**
         * 验证码
         * @param string $id   验证码ID
         */
        public static function code($id = 'code') {
            return '<img src="' . url('api/index/code/') . '" id="' . $id . '" onclick="this.src=this.src+\'?\'" style="cursor:pointer;" title="看不清？点击更换">';
        }

        /**
         * 日期时间控件
         *
         * @param $name name
         * @param $val 默认值
         * @param $isdatetime 是否显示时分秒
         * @param $loadjs 是否重复加载js，防止页面程序加载不规则导致的控件无法显示
         * @param $str 其他字符串，可加样式等
         */
        public static function datetime($name, $val = '', $isdatetime = 0, $loadjs = 0, $str = '') {
            $string = '';
            if ($loadjs || !defined('DATETIME')) {
                define('DATETIME', 1);
                $string .= '<link href="' . STATIC_URL . '/plugins/calendar/calendar.css" rel="stylesheet" type="text/css"/>'
                        . '<script src="' . STATIC_URL . '/plugins/calendar/calendar.js" type="text/javascript"></script>';
            }

            $string .= '<input class="calendar-icon form-control" style="width:118px;" onclick="showcalendar(event, this)"  value="' . $val . '" name="' . $name . '" id="' . $name . '" ' . $str . '>';

            return $string;
        }

        /**
         * 图片上传
         *
         * @param $name name
         * @param $val 默认值
         * @param $style 样式
         */
        public static function image($name, $val = '', $style = 'width:370px', $iscropper = false) {
            $string = '<input class="form-control uploadfile" type="text" name="' . $name . '"  value="' . $val . '"  onmouseover="layer_img_preview(\'' . $name . '\', this.value)" id="' . $name . '" style="' . $style . '" > '
                    . '<a href="javascript:;" onclick="layer_upload_att(\'' . url('attachment/api/upload_box', array('module' => MODULE_NAME, 'pid' => $name)) . '\')" class="btn btn-primary radius upload-btn">浏览文件</a>';

            if ($iscropper)
                $string = $string . ' ' . form::cropper($name);
            return $string;
        }

        /**
         * 多图上传
         *
         * @param $name name
         * @param $val 默认值
         * @param $n 上传数量
         */
        public static function images($name, $val = '', $n = 20) {
            $string = '';
            $string .= '<fieldset class="fieldset_list" style="width:825px;"><legend>图片列表</legend><div class="fieldset_tip">您最多可以同时上传 <span style="color:red">' . $n . '</span> 个文件</div>
			<ul id="' . $name . '" class="file_ul">';
            if ($val) {
                $arr = string2array($val);
                foreach ($arr as $key => $val) {
                    $string .= '<li>文件：<input type="text" name="' . $name . '[url][]" value="' . $val['url'] . '" id="' . $name . '_' . $key . '" onmouseover="layer_img_preview(\'' . $name . '_' . $key . '\', this.value)" onmouseout="layer.closeAll();" class="form-control w_300"> 描述：<input type="text" name="' . $name . '[alt][]" value="' . $val['alt'] . '" class="input-text w_200"><a href="javascript:;" onclick="remove_li(this);">删除</a></li>';
                }
            }
            $string .= '</ul></fieldset>
	     <a href="javascript:;" onclick="layer_upload_att(\'' . url('attachment/api/upload_box', array('module' => MODULE_NAME, 'pid' => $name, 'n' => $n)) . '\')" class="btn btn-primary radius upload-btn mt-5">浏览文件</a>';

            return $string;
        }

        /**
         * 图像裁剪
         *
         * @param $cid 		原图所在input的id
         * @param $spec  	裁剪规则，1：3*2, 2:4*3, 3:1*1
         */
        public static function cropper($cid, $spec = 1) {
            $string = '<a href="javascript:;" onclick="layer_img_cropper(\'' . $cid . '\', \'' . url('attachment/api/img_cropper', array('spec' => $spec)) . '\')" class="btn btn-info radius upload-btn">裁剪图片</a>';

            return $string;
        }

        /**
         * 附件上传
         *
         * @param $name name
         * @param $val 默认值
         * @param $style 样式
         */
        public static function attachment($name, $val = '', $style = 'width:370px') {
            $string = '<input class="form-control uploadfile" type="text" name="' . $name . '"  value="' . $val . '"  id="' . $name . '" style="' . $style . '" > <a href="javascript:;" onclick="layer_upload_att(\'' . url('attachment/api/upload_box', array('module' => MODULE_NAME, 'pid' => $name, 't' => 2)) . '\')" class="btn btn-primary radius upload-btn">浏览文件</a>';
            return $string;
        }

        /**
         * 编辑器
         *
         * @param $name name
         * @param $val 默认值
         * @param $style 样式
         * @param $isload 是否加载js,当该页面加载过编辑器js后，无需重复加载
         */
        public static function editor($name = 'content', $val = '', $style = '', $isload = true) {
            $style = $style ? $style : 'width:925px;height:350px;';
            $string = '';
            if ($isload) {
                $string .= '<link href="/static/plugins/umeditor/themes/default/css/umeditor.css" type="text/css" rel="stylesheet">
<script type="text/javascript" charset="utf-8"  src="/static/plugins/umeditor/umeditor.config.js"></script>
<script type="text/javascript" charset="utf-8"  src="/static/plugins/umeditor/umeditor.min.js"></script>
<script type="text/javascript" charset="utf-8"  src="/static/plugins/umeditor/lang/zh-cn/zh-cn.js"></script>';
            }
            $string .= '<script id="' . $name . '" type="text/plain" style="' . $style . '" name="' . $name . '">' . $val . '</script>
            <script type="text/javascript"> var ue = UM.getEditor(\'' . $name . '\'); </script>
            提取内容第一张图片为缩略图<input style="width:25px;" name="auto_thum" type="checkbox" id="readpact" value="1" checked="checked"/>
            远程图片本地化<input style="width:25px;"name="grab_img" type="checkbox" value="1"/>	';

            return $string;
        }

        /**
         * 模板选择
         *
         * @param $style  风格
         * @param $pre 模板前缀
         */
        function select_template($style, $pre = '', $id = '', $setval = '') {
            $templates = array();
            $tem_style = APP_PATH . DS . 'home' . DS . 'view' . DS . 'default' . DS . 'config.php';
            if (is_file($tem_style)) {
                $templets = require($tem_style);
                $configs = $templets[$style];
                foreach ($configs as $key => $val) {
                    $templates[$key] = $val . '(' . $key . ')';
                }
            }
            ksort($templates);
            return self::select($id, $setval, $templates, L('please_select'));
        }

    }

}

